/* File : CommandProcessor.cpp
*  Dibuat Oleh : 23512077/ Ni Made Satvika Iswari
*/

#include "CommandProcessor.h"
#include "../File/FileExp.h"

vector<command> CommandProcessor::RefCommand;

void CommandProcessor::ReadFile(string NamaFile){
/*I.S File mungkin kosong */
/*F.S Membaca file yang berisi daftar command yang valid */
	/* Kamus Lokal */
	string line;
	
	/* Algoritma */
	ifstream myfile (NamaFile.c_str());
	if (myfile.is_open()){
		while ( myfile.good() ){
			getline (myfile,line);
			DateTime D;
			command C(line, 0, D);
			CommandProcessor::RefCommand.push_back(C);
		}
		myfile.close();
	}else{
		throw (FileExp(CANNOT_OPEN));
	}
}
/* 4 sekawan */
CommandProcessor::CommandProcessor() : File() {
/* Constructor */
	// Inisialisasi Reference Command
	try{
		CommandProcessor::ReadFile("Command.txt");	//File default daftar command valid
	}catch(FileExp& f){
		f.DisplayMsg();
	}
}

CommandProcessor::CommandProcessor(vector<command> cmdList) : File() {
/* Constructor berparameter */
	UserCommand = cmdList;
}

CommandProcessor::CommandProcessor(const CommandProcessor& cmdPrc) : File() {
/* Copy constructor */
	UserCommand = cmdPrc.UserCommand;
}

CommandProcessor::~CommandProcessor() {}
/* Destructor */

CommandProcessor& CommandProcessor::operator= (const CommandProcessor& cmdPrc) {
/* Overloading operator = */
	UserCommand = cmdPrc.UserCommand;
}

bool CommandProcessor::IsValid(command cmd) {
/* Mengembalikan true jika command inputan user sesuai template command referensi pada RefCommand */
/* Pre kondisi : RefCommand sudah diinisialisasi */
	/* Kamus Lokal */
	bool found = false;
	vector<command>::iterator it;
	
	/* Algortima */
	it = CommandProcessor::RefCommand.begin();
	while (found == false && it < CommandProcessor::RefCommand.end()){
		if (*it == cmd){
			found = true;	
		}
		it++;
	};
	
	return found;
}

bool CommandProcessor::IsEmpty(){
/* Mengembalikan true jika UserCommand koosng, false jika tidak */
	return UserCommand.empty();
}

int CommandProcessor::AddCommand(command cmd){
/* Mengembalikan 1 jika command sesuai dan selesai ditambahkan, 0 jika command tidak sesuai */
	if (IsValid (cmd)){
		UserCommand.push_back(cmd);
		return 1;
	}else{
		return 0;
	}
}

int CommandProcessor::DelCommand(command cmd){
/* Pre kondisi : UserCommand tidak kosong */
/* Mengembalikan 1 jika command ditemukan dan dihapus, 0 jika tidak */
	/* Kamus Lokal */
	int found = 0;
	vector<command>::iterator it;
	
	/* Algortima */
	it = UserCommand.begin();
	do{
		if (*it == cmd){
			UserCommand.erase(it);
			found = 1;	
		}
		it++;
	}while (found == 0 && it < UserCommand.end());
	
	return found;
}

void CommandProcessor::Empty(){
/*I.S UserCommand tidak kosong */
/*F.S Mengosongkan memori yang menyimpan command dari user (UserCommand) */
	UserCommand.clear();
}

void CommandProcessor::DisplayUserCommand(){
/*I.S. UserCommand mungkin kosong */
/*F.S. Isi UserCommand ditampilkan satu per satu ke layar */
	/* Kamus Lokal */
	vector<command>::iterator it;
	
	/* Algoritma */
	if (UserCommand.empty()){
		cout<< "List command kosong."<< endl;
	}else{
		for (it = UserCommand.begin(); it < UserCommand.end(); it++){
			cout << (*it).ToString() << endl;
		}
	}
}

void CommandProcessor::DisplayRefCommand(){
/*I.S. RefCommand mungkin kosong */
/*F.S. Isi RefCommand ditampilkan satu per satu ke layar */
	/* Kamus Lokal */
	vector<string>::iterator it;
	vector<string> refCmd = CommandProcessor::getRefCommand();
	
	/* Algoritma */
	if (IsEmpty()){
		cout<< "List command kosong."<< endl;
	}else{
		for (it = refCmd.begin(); it < refCmd.end(); it++){
			cout << *it << endl;
		}
	}
}

void CommandProcessor::SaveFile(string NamaFile){
/*I.S File mungkin belum terbentuk, NamaFile sesuai dengan format */
/*F.S Menuliskan command valid dari user ke dalam sebuah file*/
	/* Kamus Lokal */
	vector<command>::iterator it;
	
	/* Algoritma */
	ofstream myfile (NamaFile.c_str());
	if (myfile.is_open()){
		for (it = UserCommand.begin(); it < UserCommand.end(); it++){
			myfile << (*it).ToString() << endl;
		}
		myfile.close();
	}else{
		throw (FileExp(CANNOT_OPEN));
	}
}

vector<string> CommandProcessor::getRefCommand()
/* Mengembalikan command referensi tanpa waktu dan tanggal */
{
	string tmp;
	vector<string> contCmd;
	
	/* Melakukan spliting vector of command dengan delimiter " ", kemudian menyimpannya dalam container vector contTemp*/
	vector<command>::iterator it;
	for (it = CommandProcessor::RefCommand.begin(); it < CommandProcessor::RefCommand.end(); it++){
		/*for (int i = 0; i < (*it).getCountArgument(); i++){
			cout<< "ARG " << (*it).getArgument(i);
			tmp.append((*it).getArgument(i));
		}*/
		//cout << "ARG : " << (*it).getArgument(0) << endl;
		contCmd.push_back((*it).getArgument(0)); 
	}
	return contCmd;
}

/* Getter */
vector<command> CommandProcessor::getUserCommand(){
/* Mengembalikan command user dengan waktu dan tanggal */
	return UserCommand;
}
